package com.cskaoyan.order.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.order.constant.OrderRetCode;
import com.cskaoyan.order.dto.CreateOrderRequest;
import com.cskaoyan.order.dto.CreateOrderResponse;
import com.cskaoyan.order.dto.OrderListRequest;
import com.cskaoyan.order.dto.OrderListResponse;
import com.cskaoyan.order.form.OrderDetail;
import com.cskaoyan.order.form.PageInfo;
import com.cskaoyan.order.form.PageResponse;
import com.cskaoyan.order.service.OrderCoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @description:
 * @author: lxc
 * @date: 2022/5/19 20:50
 */
@RequestMapping("shopping")
@RestController
public class OrderController {

    @Autowired
    OrderCoreService orderCoreService;

    @PostMapping("order")
    public ResponseData creatOrder(@RequestBody CreateOrderRequest createOrderRequest, HttpServletRequest request){

        // 获取Token中的用户信息
        String user_info = request.getHeader("user_info");
        JSONObject jsonObject = JSON.parseObject(user_info);
        Long uid = Long.parseLong(jsonObject.get("uid").toString());

        // 将uid赋给createOrderRequest
        createOrderRequest.setUserId(uid);
        CreateOrderResponse createOrderResponse = orderCoreService.createOrder(createOrderRequest);

        if (OrderRetCode.SUCCESS.getCode().equals(createOrderResponse.getCode())){
            return new ResponseUtil().setData(createOrderResponse.getOrderId(),"成功");
        }

        // 业务执行失败
        return new ResponseUtil().setErrorMsg(createOrderResponse.getMsg());
    }

    /**
     * 获取当前用户的所有订单
     * @param orderListRequest
     * @param request
     * @return
     */
    @GetMapping("order")
    public ResponseData getAllOrder(OrderListRequest orderListRequest, HttpServletRequest request){


        // 获取Token中的用户信息
        String user_info = request.getHeader("user_info");
        JSONObject jsonObject = JSON.parseObject(user_info);
        Long uid = Long.parseLong(jsonObject.get("uid").toString());

        orderListRequest.setUserId(uid);

        OrderListResponse orderListResponse = orderCoreService.queryAllOrders(orderListRequest);

        if (OrderRetCode.SUCCESS.getCode().equals(orderListResponse.getCode())){
            PageResponse pageResponse = new PageResponse();
            pageResponse.setData(orderListResponse.getDetailInfoList());
            pageResponse.setTotal(orderListResponse.getTotal());
            return new ResponseUtil().setData(pageResponse);
        }

        // 业务执行失败
        return new ResponseUtil().setErrorMsg(orderListResponse.getMsg());
    }

    @GetMapping("order/{id}")
    public ResponseData getOrderDatail(@PathVariable("id") Long id,HttpServletRequest request){

        // 获取Token中的用户信息
        String user_info = request.getHeader("user_info");
        JSONObject jsonObject = JSON.parseObject(user_info);
        Long uid = Long.parseLong(jsonObject.get("uid").toString());
        String username = (String) jsonObject.get("username");

        OrderDetail orderDetail = orderCoreService.queryOrderDetail(id,username,uid);

        return new ResponseUtil().setData(orderDetail);
    }
}
